// pages/list/list.js
const app = getApp();
const util = require('../../utils/util.js');
const { $Toast } = require('../../dist/base/index');
var pageNo = 1;
var specialTypeUrl='';
Page({
  data: {
    basePath:util.basePath,
    tip: "",
    loading: false,
    content: [],
    
    showhaibao:false,
    rease:"",
    deleteId:0,
    showhaibao3:false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    specialTypeUrl = options.id;
    console.log(specialTypeUrl)
    // wx.setNavigationBarTitle({
    //   title: options.specialTypeUrl
    // })

    //加载  
    var that = this;
    this.getIndexList();
  },
  details(e) {
    //详情页跳转
    console.log(e.currentTarget.id)
    wx.navigateTo({
      url: '../mySpecialDetails/mySpecialDetails?id=' + e.currentTarget.id
    })

  },
  editPost(e) {
    //详情页跳转
    console.log(e.currentTarget.id)
    wx.navigateTo({
      url: '../mySpecials/mySpecials?id=' + e.currentTarget.id
    })

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    wx.showLoading({ title: '刷新中', });
    //用户下拉刷新
    this.data.content = [];
    pageNo = 1;
    this.getIndexList();
    //停止当前页面下拉刷新。
    wx.stopPullDownRefresh()
    setTimeout(function () {
      wx.hideLoading()
    }, 1500)
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    var that = this;
    that.setData({ loading: true, tip: "正在加载" })
    setTimeout(function () {
      that.setData({ loading: false, tip: "没有更多数据" })
    }, 10);
  },
  /**
    * 页面相关事件处理函数--监听用户下拉动作
    */
  onPullDownRefresh: function () {
    // 显示顶部刷新图标
    wx.showNavigationBarLoading();
    this.data.content = [];
    pageNo = 1;
    this.getIndexList();

    setTimeout(function () {
      // 隐藏导航栏加载框
      wx.hideNavigationBarLoading();
      //停止当前页面下拉刷新。
      wx.stopPullDownRefresh()
    }, 1500)
  },
  // /**
  //  * 用户点击右上角分享
  //  */
  // onShareAppMessage: function () {
  //   if (res.from === 'button') {
  //     // 来自页面内转发按钮
  //     console.log(res.target)
  //   }
  //   return {
  //     title: '上研党建云',
  //     path: '/pages/index/index'
  //   }
  // }, 
  getIndexList: function () {
    var that = this;
    that.setData({ loading: true, tip: "正在加载" })
    console.log(pageNo);
    console.log(this.data.content);
    //获取分类文章列表
    var that = this;
    var _loginInfo = wx.getStorageSync('loginInfo');
    wx.request({
      url: util.basePath + '/api/specials/mySpecial/pages/' + pageNo,
      method:"post",
      data: {
        whiteId:_loginInfo.whiteId,
        openId:_loginInfo.openId
      },   
      dataType: 'json',
      header: {
        'content-type': 'application/json'
      },
      success(res) {
        console.log('获取分类文章列表')
        if (res.statusCode == 200) {
          console.log(res.data);
          if (typeof res.data != 'object') {
            console.log('11111')
            var jsonStr = res.data.replace('\n', '');//重点
            var jj = JSON.parse(jsonStr);
            res.data = jj;
          } else {
            console.log('22222');
          }

          if (res.data.code == 200 && res.data.result.content!=null) {
            console.log('接口返回数据加载数据');
            console.log(res.data.result);

              that.setData({
                // 分页追加数据
                'content': that.data.content.concat(res.data.result.content)
              });
            if (res.data.result.last==true){
                    setTimeout(function () {
                      that.setData({ loading: false, tip: "没有数据了" })
                    }, 10);
            }

          } 

        } else {
          $Toast({ content: '请求错误', type: 'error' });
        }

      }
    })
  },
  // writeSpecial:function(){
  //   wx.navigateTo({
  //     url: '../specialIndex/specialIndex?id=' + specialTypeUrl,
  //   })
  // },
  unPassRease(e) {
    //详情页跳转
    var _rease = e.currentTarget.dataset.content;
    console.log(_rease);
    this.setData({showhaibao:true,
      rease:_rease
    });
  },

  cancelTanKuang(){
    this.setData({showhaibao:false});
  },
  
  deletePost(e) {
    //详情页跳转
   
    var _postId = e.currentTarget.id;
    console.log(_postId);
    this.setData({showhaibao3:true,
      deleteId:_postId
    });
  },


  cancelTanKuang3(){
    this.setData({showhaibao3:false});
  },



  bindFormSubmit: function(e) {
    //详情页跳转
   
    var _postId = this.data.deleteId;
    console.log(_postId);
    var that = this;
    // if(this.data.isSetRead == true){
    //   return;
    // }
    // this.data.isSetRead = true;
    var _loginInfo = wx.getStorageSync('loginInfo');
    wx.request({
      url: util.basePath + '/api/specials/deleteSpecial',
      method:"post",
      data: {
        whiteId:_loginInfo.whiteId,
        openId:_loginInfo.openId,
        postId:_postId
      },
      success: res => {
        console.log(res);
        if(res.data.code==200){
          var postId = res.data.result;
          wx.redirectTo({
            url: '/pages/mySpecialList/mySpecialList',
          })
          
          // this.getIndexList();
          
        } else if(res.data.code == 500) {
            
          $Toast({
            content: res.data.msg,
            type: 'error'
          });
        }else{
            $Toast({ content: '请求错误', type: 'error' });
        }
      }
    })
  }, 
  
  
})